package cn.uniquetech.esf;

import cn.uniquetech.util.FileUtil;
import cn.uniquetech.util.SqlJavaType;
import cn.uniquetech.util.StrUtil;

import java.util.List;

public class GenVOUtil {

	// 要排除的字段
	private static String fields = ",iid,icreator,dcreator,ccreator,imodify,dmodify,cmodify,";

	public static String genVOStr(List<ColumnVO> columnList) {
		ColumnVO columnVO = columnList.get(0);
		String voPackage = GenerateCodeSqlServer.PACKAGENAME + ".vo";
		StringBuffer sb = new StringBuffer();
		sb.append("package " + voPackage + ";\r\n");
		sb.append("\r\n");
		sb.append("import cn.uniquesoft.vo.base.BaseVO;\r\n");

		String clazz = columnVO.getVoName();
		sb.append("/**" + columnVO.getTbRemarks() + "**/\r\n");
		sb.append("@SuppressWarnings(\"serial\")" + "\r\n");
		sb.append("public class " + clazz + "VO" + " extends BaseVO" + " {\r\n");

		for (ColumnVO item : columnList) {
			if (fields.indexOf("," + item.getColName() + ",") == -1) {
				sb.append("\t//" + item.getColRemarks() + "\r\n");
				sb.append("\tprivate " + SqlJavaType.sqlType2JavaType(item.getColType()) + " " + item.getColName() + ";\r\n");
			}
		}

		for (ColumnVO item : columnList) {
			if (fields.indexOf("," + item.getColName() + ",") == -1) {
				sb.append(genSetGet(SqlJavaType.sqlType2JavaType(item.getColType()), item.getColName()));
			}
		}

		sb.append("}\r\n");
		FileUtil.writeFile(voPackage, clazz + "VO" + ".java", sb.toString());
		return sb.toString();
	}

	// 生成set get
	private static String genSetGet(String colType, String colName) {
		String str = "";
		String capName = StrUtil.captureName(colName);
		str += "\tpublic " + colType + " get" + capName + "(){return this." + colName + ";}" + "\r\n";
		str += "\tpublic void set" + capName + "(" + colType + " " + colName + "){this." + colName + "=" + colName + ";}" + "\r\n";
		return str;
	}
}
